home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Tools / nShell™ 1.0.3 / src / Programmer's Guide / 04 Example Code < prev    next >
Encoding:
Text File  |  1994-09-04  |  2.2 KB  |  71 lines  |  [TEXT/ttxt]

  1. 04 An Example Command - The Code
  2. ================================
  3.  
  4. The text of the “hello” command is show below:
  5.  
  6. #include "nshc.h"
  7.  
  8. void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  9. {
  10.     if (!nshc_bad_version( nshc_parms, nshc_calls, NSHC_VERSION )) {
  11.     
  12.         // otherwise, handle requests from the application
  13.     
  14.           switch (nshc_parms->action) {
  15.             case nsh_start:
  16.                 nshc_calls->NSH_printf("Hello World\r");
  17.                 break;
  18.             case nsh_continue:
  19.             case nsh_idle:
  20.             case nsh_stop:
  21.                 break;
  22.             }
  23.         
  24.         // tell the application we are done
  25.  
  26.          nshc_parms->action = nsh_idle;
  27.         nshc_parms->result = NSHC_NO_ERR;
  28.  
  29.         }
  30. }
  31.  
  32. The include file:
  33.  
  34. #include "nshc.h"
  35.  
  36. defines the interface between the nShell application and its external commands.  The file contains the prototype for the command “main” and the definitions for the “nshc_parms” and “nshc_calls” parameter blocks which are used in that call:
  37.  
  38. void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  39.  
  40. The call:
  41.  
  42.     if (!nshc_bad_version(nshc_parms, nshc_calls, NSHC_VERSION))
  43.  
  44. is to a standard routine in the file "nshc_utl.c".  This routine confirms that the same versions of nshc.h were used to build the nShell application and this command.  If there is a version mismatch, nshc_bad_version sets the command action to nsh_idle and the command result to NSHC_ERR_VERSION.
  45.  
  46. The switch statement is used to interpret the “action” message from the application:
  47.  
  48.      switch (nshc_parms->action) {
  49.         case nsh_start:
  50.             nshc_calls->NSH_printf("Hello World\r");
  51.             break;
  52.         case nsh_continue:
  53.         case nsh_idle:
  54.         case nsh_stop:
  55.             break;
  56.         }
  57.  
  58. Whenever this command is sent an nsh_start, it prints a “Hello World” message.  When it is sent an nsh_continue, nsh_idle or nsh_stop message, it takes no action.
  59.  
  60. The printf call is made through the nshc_calls block:
  61.  
  62.     nshc_calls->NSH_printf("Hello World\r");
  63.  
  64. This internal version of printf is compatible with nShell I/O redirection.
  65.  
  66. In any case, the command considers itself done, and sets the action to nsh_idle and the result to NSHC_NO_ERR (ie. zero):
  67.  
  68.     nshc_parms->action = nsh_idle;
  69.     nshc_parms->result = NSHC_NO_ERR;
  70.  
  71. If this were not done, the application would call the command repeatedly waiting for the action became nsh_idle, resulting in an endless loop.